package com.green.web.controller.manage;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.green.common.page.Pagination;
import com.green.model.manage.ErpPackage;
import com.green.model.manage.enums.JoinTypeEnum;
import com.green.model.manage.enums.StateEnum;
import com.green.service.common.ServiceContext;
import com.green.service.manage.ErpPackageService;
import com.green.web.controller.common.BaseController;

/**
 * erp加盟套餐
 * @author 谷辉
 *
 */
@Controller
@RequestMapping(value = "/erpPackage/*")
public class ErpPackageController extends BaseController{

	@Resource
	private ErpPackageService erpPackageService;		

	
	@RequestMapping(value = "manager")
	public String manager(ModelMap modelMap) {
		modelMap.put("joinTypeEnum", JoinTypeEnum.toList());//套餐类型
		return "manage/ErpPackageManager";
	}
	/**
	 * @param modelMap套餐添加页面
	 * @return
	 */
	@RequestMapping(value = "add")
	public String add(ModelMap modelMap) {
		modelMap.put("joinTypeEnum", JoinTypeEnum.toList());//套餐类型	
		
		return "manage/ErpPackageAdd";
	}
	
	/**
	 * @param modelMap保存套餐设置
	 * @return
	 */
	@RequestMapping(value = "save")
	@ResponseBody
	public Map<String, Object> save(ModelMap modelMap,ErpPackage erpPackage) {
		erpPackage.setCreateBy(ServiceContext.getLoginPerson());
		erpPackage.setId(UUID.randomUUID().toString());	
		erpPackage.setStatus(StateEnum.DISABLED);
		int count =erpPackageService.insertErpPackage(erpPackage);
		Map<String, Object> result = new HashMap<String, Object>();
		result.put("message", count);
		return result;
	}
	/**
	 * 查询套餐数据
	 * 
	 * @param modelMap
	 * @return
	 */
	@RequestMapping("listData")
	@ResponseBody
	public void listData(Pagination<ErpPackage> page,HttpServletResponse response) {
		Map<String, Object> param = new HashMap<String, Object>();	
		param.put("joinType", getString("joinType"));
		page =erpPackageService.getErpPackagePagination(page, param);
        
		outPrint(response, JSONObject.fromObject(page, getDefaultJsonConfig()));
	}
	/**
	 * 禁用 
	 * 启用
	 * @return
	 */
	@RequestMapping("updateErpPackage")
	@ResponseBody
	public Map<String, Object> updateErpPackage() {
		String id = getString("id");
		int type = getInt("type");
		int count = 0;
		if (null != id && !id.equals("")) {
			ErpPackage erpPackage=erpPackageService.getErpPackageById(id);
			erpPackage.setLastUpdateBy(ServiceContext.getLoginPerson());

			if(type==1){
				erpPackage.setStatus(StateEnum.DISABLED);
				count=erpPackageService.updateErpPackage(erpPackage);
			}else{
				erpPackage.setStatus(StateEnum.EFFICTIVE);	
				Map<String, Object> param = new HashMap<String, Object>();
				param.put("positionId", erpPackage.getPositionId());
				param.put("limitCity", erpPackage.getLimitCity());
				param.put("joinType", erpPackage.getType());
				param.put("status", StateEnum.EFFICTIVE);
				List<ErpPackage> list=erpPackageService.getErpPackageList(param);
				if(null==list||list.size()>0){
					count=9;
				}else{
					count=erpPackageService.updateErpPackage(erpPackage);
				}
			}
			
		}
		Map<String, Object> result = new HashMap<String, Object>();
		result.put("message", count);
		return result;
	}
}
